package com.zonski.jbots.game.resources;

import com.zonski.jbots.engine.*;
import com.zonski.jbots.engine.entity.MovingEntity;

import com.zonski.jbots.game.EntityHelper;
import com.zonski.jbots.game.entity.EggMind;

import com.zonski.jbots.game.JBotsConstants;

public class EggMaker extends UsableResource
{
    private static final int DEFAULT_CHARGE_INCREMENT = 1;
    private static final int DEFAULT_CHARGE_DIV = 1;

    private int chargeIncrement = DEFAULT_CHARGE_INCREMENT;
    private int chargeDiv = DEFAULT_CHARGE_DIV;
    private int charge;
    protected int minCharge;
    protected int maxCharge;
    private String toCreate;
    private boolean createNextRound;
    private int numUpdates;

    public EggMaker()
    {
    }

    public int getCharge()
    {
        return this.charge;
    }

    public int getChargeIncrement()
    {
        return this.chargeIncrement;
    }

    public int getChargeDiv()
    {
        return this.chargeDiv;
    }

    public int getMinCharge()
    {
        return this.minCharge;
    }

    public void setMinCharge(int minCharge)
    {
        this.minCharge = minCharge;
    }

    public int getMaxCharge()
    {
        return this.maxCharge;
    }

    protected int getMaxCharge(String toCreate)
    {
        return this.maxCharge;
    }

    public void setMaxCharge(int maxCharge)
    {
        this.maxCharge = maxCharge;
    }

    public String getToCreate()
    {
        return this.toCreate;
    }

    public void setToCreate(String toCreate)
    {
        this.toCreate = toCreate;
    }

    public void startUsing(int direction)
    {
        super.startUsing(direction);
        /*
        if(this.toCreate != null)
        {
            System.out.println(this.getOwner().layerId+" is starting to create a "+this.toCreate);
        }
        */
    }

    public void stopUsing()
    {
        if(this.isBeingUsed())
        {
            // create an egg
            createNextRound = true;
            /*
            if(this.toCreate != null)
            {
                System.out.println(this.getOwner().layerId+" has stopped using "+this.toCreate);
            }
            */
        }
        super.stopUsing();
    }

    public void update(Engine engine)
        throws Exception
    {
        this.numUpdates++;
        super.update(engine);
        if(this.isBeingUsed())
        {
            if(this.numUpdates % this.chargeDiv == 0)
            {
                this.charge += this.chargeIncrement;
                int ownerCharge = this.getOwner().getCharge();
                if(this.charge >= ownerCharge)
                {
                    this.charge = ownerCharge - 1;
                }
                this.charge = Math.min(this.maxCharge, this.charge);
            }
        }
        if(createNextRound)
        {
            createNextRound = false;
            if(this.charge >= this.minCharge && !EntityHelper.isOccupied(this.getOwner(), engine, JBotsConstants.COMET_ID))
            {
                create(engine);
            }
            this.charge = 0;
        }
    }

    public void create(Engine engine)
        throws Exception
    {
        Entity parent = this.getOwner();
        parent.setCharge(parent.getCharge() - this.charge);

        EntityFactory entityFactory = engine.getEntityFactory();
        Entity entity = entityFactory.createEntity(JBotsConstants.EGG);
        EggMind mind = new EggMind();
        String toCreate = this.getToCreate();
        mind.setToCreate(toCreate);
        ((MovingEntity)entity).setMind(mind);
        mind.setEntity(entity);
        //entity.bounds.setCentre(this.getOwner().bounds);
        EntityHelper.centreAtClosestTile(entity.bounds,
                this.getOwner().bounds, engine.getRoom());
        entity.layerId = this.getOwner().layerId;
        int charge = entity.getCharge() + this.charge;
        entity.setMaxCharge(charge);
        entity.setCharge(charge);

        engine.addEntity(entity);
    }

    public Resource copy()
    {
        EggMaker eggMaker = new EggMaker();
        eggMaker.setMinCharge(this.minCharge);
        eggMaker.setToCreate(this.toCreate);
        eggMaker.setMaxCharge(this.maxCharge);
        return eggMaker;
    }
}
